package com.whfc.common.third.energy;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.whfc.common.util.DateUtil;
import com.whfc.common.util.HttpUtil;
import com.whfc.common.util.JSONUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.*;

/**
 * @Description 恒焊水电表工具类
 * @Author hw
 * @Date 2021-09-16 9:04
 * @Version 1.0
 * http://doc-api.tqdianbiao.com/#/api1/2/2_1
 */
public class TQUtils {

    private static Logger logger = LoggerFactory.getLogger(TQUtils.class);

    private static final String url = "https://iot.tqdianbiao.com/Api/DataRequest";

    private static final String authCode = "cabc9f9f04fa686b766b6903e3068891";

    private static final String SUCCESS = "1";

    /**
     * 查询水电表历史数据接口
     *
     * @return
     * @throws UnsupportedEncodingException
     */
    @Deprecated
    public static List<TQResultDTO> historicalData() throws UnsupportedEncodingException {
        Date endTime = new Date();
        Date startTime = DateUtil.addHours(endTime, -1);


        Map<String, String> params = new HashMap<>();
        params.put("auth", authCode);
        params.put("type", "json");
        params.put("functionids", "3,4,5,6,15,16,17,18,2227,28,29,30,31,32,33,34,42");
        params.put("start_time", URLEncoder.encode(DateUtil.formatDate(startTime, DateUtil.DATE_TIME_FORMAT), "UTF-8"));
        params.put("end_time", URLEncoder.encode(DateUtil.formatDate(endTime, DateUtil.DATE_TIME_FORMAT), "UTF-8"));

        String rep = HttpUtil.doGet(url, null, params);
        try {
            JSONObject repJson = JSONObject.parseObject(rep);
            logger.info("查询水电表历史数据返回数据，rep:{}", rep);
            if ("1".equals(repJson.getString("status"))
                    && repJson.containsKey("data")
                    && repJson.getJSONArray("data").size() > 0) {
                return repJson.getJSONArray("data").toJavaList(TQResultDTO.class);
            }

        } catch (Exception e) {
            logger.error("查询水电表历史数据失败", e);
        }
        return null;
    }

    /**
     * 获取电表历史数据
     *
     * @return
     */
    public static List<TQResultDTO> getElectricHistoryData() {
        String fids = StringUtils.join(TQConst.ELECTRIC_FIDS.keySet(), ",");
        return getHistoryData(fids);
    }

    /**
     * 获取水表历史数据
     *
     * @return
     */
    public static List<TQResultDTO> getWaterHistoryData() {
        String fids = StringUtils.join(TQConst.WATER_FIDS.keySet(), ",");
        return getHistoryData(fids);
    }

    /**
     * 查询历史数据
     *
     * @param fids
     * @return
     */
    public static List<TQResultDTO> getHistoryData(String fids) {
        Date endTime = new Date();
        Date startTime = DateUtil.addHours(endTime, -1);
        try {
            Map<String, String> params = new HashMap<>(5);
            params.put("auth", authCode);
            params.put("type", "json");
            params.put("functionids", fids);
            params.put("start_time", URLEncoder.encode(DateUtil.formatDateTime(startTime), "UTF-8"));
            params.put("end_time", URLEncoder.encode(DateUtil.formatDateTime(startTime), "UTF-8"));

            String rep = HttpUtil.doGet(url, null, params);
            JSONObject repJson = JSONObject.parseObject(rep);
            logger.info("查询水电表历史数据返回数据，rep:{}", rep);
            if (SUCCESS.equals(repJson.getString("status"))) {
                JSONArray dataArr = repJson.getJSONArray("data");
                if (dataArr.size() > 0) {
                    return dataArr.toJavaList(TQResultDTO.class);
                }
            }

        } catch (Exception e) {
            logger.error("查询水电表历史数据失败", e);
        }
        return Collections.emptyList();
    }

    public static void main(String[] args) {
        List<TQResultDTO> list1 = getElectricHistoryData();
        List<TQResultDTO> list2 = getWaterHistoryData();
        System.out.println(JSONUtil.toPrettyString(list1));
        System.out.println(JSONUtil.toPrettyString(list2));
    }
}
